package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.ProveNumDao;
import Entity.ProveNumEntity;

public class ProveNumListServlet extends HttpServlet {
	
	private ProveNumDao numdao = new ProveNumDao();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		String path = "pages/ProveNum_List.jsp";
		boolean ac = false;
		if(method.equals("list")){
			List<ProveNumEntity>list = numdao.select();
			int pay_num = list.get(0).getPay_num();
			int refund_num = list.get(0).getRefund_num();
			int id = list.get(0).getId();
			request.setAttribute("id", id);
			request.setAttribute("pay_num", pay_num);
			request.setAttribute("refund_num", refund_num);
		}else if(method.equals("update")){
			if(request.getParameter("id").equals("1")){
				numdao.update_pay(Integer.parseInt(request.getParameter("name")));
			}else{
				numdao.update_refund(Integer.parseInt(request.getParameter("name")));
			}
			path = "ProveNum_List_Servlet?method=list";
			ac = true;
		}
		if(ac){
			response.sendRedirect(path);
		}else{
			request.getRequestDispatcher(path).forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
